package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TbCounselor;
import com.zhentao.pojo.TbTeacher;
import com.zhentao.pojo.TbUser;
import com.zhentao.service.TbCounselorService;
import com.zhentao.service.TbTeacherService;
import com.zhentao.service.TbUserService;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private TbUserService tbUserService;
    @Autowired
    private TbCounselorService tbCounselorService;
    @Autowired
    private TbTeacherService tbTeacherService;
    @RequestMapping("/login")
    public Result login(@RequestBody TbUser user){
        QueryWrapper<TbUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        TbUser tbUser=tbUserService.getOne(queryWrapper);
        if(tbUser.getRoleId()==4){
            tbUser.setRname("管理员/院长");
            return tbUser!=null?Result.OK(tbUser):Result.ERROR();
        }
        if (tbUser.getRoleId()==3){
            QueryWrapper<TbCounselor> wrapper=new QueryWrapper<>();
            wrapper.eq("user_id",tbUser.getId());
            TbCounselor tbCounselor=tbCounselorService.getOne(wrapper);
            tbUser.setTbCounselor(tbCounselor);
            QueryWrapper<TbTeacher> wrapper1=new QueryWrapper<>();
            wrapper1.eq("user_id",tbUser.getId());
            TbTeacher tbTeacher=tbTeacherService.getOne(wrapper1);
            tbUser.setTbTeacher(tbTeacher);
            tbUser.setRname("主任");
            return tbUser!=null?Result.OK(tbUser):Result.ERROR();
        }
        if (tbUser.getRoleId()==2){
            QueryWrapper<TbCounselor> wrapper=new QueryWrapper<>();
            wrapper.eq("user_id",tbUser.getId());
            TbCounselor tbCounselor=tbCounselorService.getOne(wrapper);
            tbUser.setTbCounselor(tbCounselor);
            tbUser.setRname("导员");
            return tbUser!=null?Result.OK(tbUser):Result.ERROR();
        }
        if (tbUser.getRoleId()==1){
            QueryWrapper<TbTeacher> wrapper=new QueryWrapper<>();
            wrapper.eq("user_id",tbUser.getId());
            TbTeacher tbTeacher=tbTeacherService.getOne(wrapper);
            tbUser.setTbTeacher(tbTeacher);
            tbUser.setRname("讲师");
            return tbUser!=null?Result.OK(tbUser):Result.ERROR();
        }
        return Result.ERROR("获取角色色错误");
    }
}
